local hx__yuanheng = fk.CreateSkill {
  name = "hx__yuanheng",
  tags = { Skill.Compulsory, },
}

hx__yuanheng:addEffect(fk.CardUsing, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__yuanheng.name) and data.card.type == Card.TypeEquip then
      return true
    else
      local use_events = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        if e.id < player.room.logic:getCurrentEvent().id then
          return e.data.from == player
        end
      end, 0, Player.HistoryGame)
      return #use_events == 1 and use_events[1].data.card.type == data.card.type and player:hasSkill(hx__yuanheng.name) and target == player
      and data.card.type == Card.TypeTrick and data.card.skillName ~= hx__yuanheng.name
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"hx__yuanheng_slash", "hx__yuanheng_tree"}
    if data.card.type == Card.TypeEquip then
      room:addPlayerMark(player, "@hx__yuanheng_equip", 1)
      room:addCardMark(data.card, "@@hx__yuanheng-phase")
      local choose = room:askToChoice(player, {
        choices = choices,
        skill_name = hx__yuanheng.name,
      })
      if choose == "hx__yuanheng_slash" then
        local to = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(player),
          min_num = 1,
          max_num = 1,
          prompt = "#hx__yuanheng-choose",
          skill_name = hx__yuanheng.name,
          cancelable = true,
        })
        if #to == 0 then return end
        if #to > 0 then
            room:useVirtualCard("slash", nil, player, to[1], hx__yuanheng.name, true)
        end
      elseif choose == "hx__yuanheng_tree" then
        if not player:isNude() then
          room:useVirtualCard("bogus_flower", nil, player, player, hx__yuanheng.name, true)
        end
      end
    elseif data.card.type == Card.TypeTrick then
      room:addPlayerMark(player, "@hx__yuanheng_trick", 1)
      room:addCardMark(data.card, "@@hx__yuanheng-phase")
      local choose = room:askToChoice(player, {
        choices = choices,
        skill_name = hx__yuanheng.name,
      })
      if choose == "hx__yuanheng_slash" then
        local to = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(player),
          min_num = 1,
          max_num = 1,
          prompt = "#hx__yuanheng-choose",
          skill_name = hx__yuanheng.name,
          cancelable = true,
        })
        if #to == 0 then return end
        if #to > 0 then
            room:useVirtualCard("slash", nil, player, to[1], hx__yuanheng.name, true)
        end
      elseif choose == "hx__yuanheng_tree" then
        if not player:isNude() then
          room:useVirtualCard("bogus_flower", nil, player, player, hx__yuanheng.name, true)
        end
      end
    end
    if (player:getMark("@hx__yuanheng_equip") - player:getMark("@hx__yuanheng_trick") == 2) or (player:getMark("@hx__yuanheng_trick") - player:getMark("@hx__yuanheng_equip") == 2) then
      local cards = table.filter(room.draw_pile, function (id)
        return Fk:getCardById(id):getMark("@@hx__yuanheng-phase") ~= 0
      end)
      table.insertTable(cards, table.filter(room.discard_pile, function (id)
        return Fk:getCardById(id):getMark("@@hx__yuanheng-phase") ~= 0
      end))
      for _, p in ipairs(room:getAllPlayers()) do
        table.insertTable(cards, table.filter(p:getCardIds("he"), function (id)
          return Fk:getCardById(id):getMark("@@hx__yuanheng-phase") ~= 0
        end))
      end
      if #cards > 0 then
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, hx__yuanheng.name, nil, false, player.id)
      end
      room:setPlayerMark(player, "@hx__yuanheng_equip", 0)
      room:setPlayerMark(player, "@hx__yuanheng_trick", 0)
      room:invalidateSkill(player, hx__yuanheng.name, "-turn")
    end
  end,
})

return hx__yuanheng